export class Message{
    private static noticeList:Map<string,Array<any>|any>=new Map();
    private static groupList:Map<string,Map<string,Array<any>|any>>=new Map();
    private static nocesList:Map<string,any>=new Map();

    static sendOnce(msg:string,...params:any){
        let item:any=Message.nocesList.get(msg);
        if(item){
            let call:any = item["fun"]; 
            const caller:any=item["call"]
            call?.call(caller,...params) 
            Message.removeOnce(msg)
        }    
    }
    static addOnce(msg:string,callFun:any,caller:any){
        let item:any=Message.nocesList.get(msg);
        if(!item){
            Message.nocesList.set(msg,{
                callFun:callFun,
                caller:caller,
            });
        }
    }
    static removeOnce(msg:string){
        let item:any=Message.noticeList.get(msg);
        if(item){
            item=null;
            Message.noticeList.delete(msg);
        }  
    }

    /** 发布通知 */
    static send(msg:string,...params:any){
        let list:Array<any>=Message.noticeList.get(msg);
        if(list){
            for(let i=0;i<list.length;i++){
                let item:any=list[i];  
                if(item){
                    let call:any = item["fun"]; 
                    const caller:any=item["call"]
                    call?.call(caller,...params)
                } 
                 
            }
        }     
    }  
    /** 监听通知 */
    static add(msg:string,callFun:any,caller:any){
        let list:Array<any>|undefined=Message.noticeList.get(msg);
        if(!list){
            list=[];
            Message.noticeList.set(msg,list);
        }
        list.push({call:caller,fun:callFun});    
    } 
    /** 移除通知监听 */
    static remove(msg:string,callFun:any,caller:any){
        let list:any=Message.noticeList.get(msg);
        if(list){
            for(let i=0;i<list.length;i++){
                if(list[i].fun == callFun && list[i].call == caller){  
                    list.splice(i,1);  
                    break; 
                }
            }
        }  
    }

    static addGroup(group:string,msg:string,callFun:any,caller:any){
        let groupList:Map<string,Array<any>|any>=Message.groupList.get(group)
        if(!groupList){
            groupList=new Map();
            Message.groupList.set(group,groupList)
        }
        let list:Array<any>|undefined=groupList.get(msg);
        if(!list){
            list=[];
            groupList.set(msg,list);
        }
        list.push({call:caller,fun:callFun}); 
    }

    static sendGroup(group:string,msg:string,...params:any){
        let groupList:Map<string,Array<any>|any>=Message.groupList.get(group)
        if(groupList){
            let list:Array<any>|undefined=groupList.get(msg);
            if(list){
                for(let i=0;i<list.length;i++){
                    let item:any=list[i];  
                    if(item){
                        let call:any = item["fun"]; 
                        const caller:any=item["call"]
                        call?.call(caller,...params)
                    } 
                     
                }
            }     
        }
    }
    static removeGroupMsg(group:string,msg:string,callFun:any,caller:any){
        let groupList:Map<string,Array<any>|any>=Message.groupList.get(group)
        if(groupList){
            let list:Array<any>|undefined=groupList.get(msg);
            if(list){
                for(let i=0;i<list.length;i++){
                    if(list[i].fun == callFun && list[i].call == caller){  
                        list[i].fun=null
                        list[i].call= null
                        list[i]=null
                        list.splice(i,1);  
                        break; 
                    }
                }
            }
        }
    }
    static removeGroup(group:string){
        let groupList:Map<string,Array<any>|any>=Message.groupList.get(group)
        if(groupList){
            groupList.clear();
            groupList=null
            Message.groupList.delete(group)
        }
    }
}